package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.dao.CheckGroupDao;
import com.entity.PageResult;
import com.entity.QueryPageBean;
import com.entity.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pojo.CheckGroup;
import com.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass =com.service.CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    //<!--新增检查组-->
    @Override
    public Result addCheckGroup(CheckGroup checkGroup, Integer[] list) {
       //数据校验
        //校验数据有无问题
        String name = checkGroup.getName();
        String code = checkGroup.getCode();
        String sex = checkGroup.getSex();
        String helpCode = checkGroup.getHelpCode();
        if (name != null && code != null &&  sex != null &&helpCode!=null) {
            if ("".equals(name) && "".equals(code) && "".equals(helpCode) && "".equals(sex)) {
                return new Result(false, "新增数据不能为空");
            } else {
                String a1 = "^\\d{4}$";
                boolean b1 = code.matches(a1);
                if (!b1) {
                    return new Result(false, "项目编码格式错误");
                }
                if (name.length() > 32) {
                    return new Result(false, "项目名称过长");
                }
                String a2 = "^[A-Z]+$";
                boolean b2 = helpCode.matches(a2);
                if (!b2) {
                    return new Result(false, "助记有误");
                }
            }
        } else {
            return new Result(false, "新增数据不能为空");
        }
        try {
            //<!--新增检查组-->
            checkGroupDao.addCheckGroup(checkGroup);
            Integer id = checkGroup.getId();
            //<!--新增中间表-->
            if (id!=null){
                int i = checkGroupDao.addGroupAndItem(id, list);
                return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
            }
        } catch (Exception e) {
            throw e;
        }

    }
    //查询所有的数据
    @Override
    public PageResult findAllCheckGroup(QueryPageBean bean) {
        Integer c = bean.getCurrentPage();
        Integer p = bean.getPageSize();
        PageHelper.startPage(c,p);
        Page<CheckGroup> page = checkGroupDao.findAllCheckGroup(bean.getQueryString());
        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();
        return new PageResult(total,rows);
    }
    //删除指定检查组,需要删除三张表中的数据
    @Override
    public Result deleteCheckGroup(int id) {
        Result result=null;
        try {
            int i = checkGroupDao.deleteGroupAndItem(id);
            int i1 = checkGroupDao.deleteCroupAndSetmeal(id);
            int i2 = checkGroupDao.deleteCheckGroup(id);
            if (i2!=0){
                //删除成功
                result=new  Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }else {
                result=new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result=new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
            throw e;
        } finally {
            return result;
        }

    }
    //修改检查组,需要先把中间表相关数据干掉,再插入中间表数据
    @Override
    public Result updateCheckGroup(CheckGroup checkGroup,Integer[] list) {
        //校验
        if (checkGroup==null||list==null){
            return new Result(false,"对象数据不能为空");
        }
        String name = checkGroup.getName();
        String code = checkGroup.getCode();
        String sex = checkGroup.getSex();
        String helpCode = checkGroup.getHelpCode();
        if (name==null||code==null||sex==null||helpCode==null){
            return new Result(false,"对象数据不能为空");
        }
        //修改主表
        int i = checkGroupDao.updateCheckGroup(checkGroup);
        //先把中间表相关数据干掉,再插入中间表数据
        checkGroupDao.deleteGroupAndItem(checkGroup.getId());
        Result result = addGroupAndItem(checkGroup.getId(), list);
        return result;
    }
    //根据gid查询所有有关的链表item数据
    @Override
    public Result findItemByGid(Integer gid) {
        if (gid==null){
            return new Result(false,"被黑客攻击,赶紧解决");
        }
        List<Integer> list = checkGroupDao.findItemByGid(gid);
        return new Result(true,"查询成功",list);
    }

    //<!--新增中间表抽取-->
    private Result addGroupAndItem(Integer id, Integer[] list){
            //<!--新增中间表-->
            if (id!=null&&list!=null&&list.length>0){
                int i = checkGroupDao.addGroupAndItem(id, list);
                return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
            }else {
                return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
            }

    };


}
